package com.etv.launcher.ui.component.status;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.provider.Settings;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.etv.launcher.R;

public class BluetoothComponent extends BaseStatusBarComponent {
    private ImageView ivIcon;
    private GradientDrawable defaultBg;
    private GradientDrawable selectedBg;

    public BluetoothComponent(Context context) {
        super(context);
    }

    @Override
    public void initView(Context context) {
        // 根容器（保证正方形）
        FrameLayout rootLayout = new FrameLayout(context);
        rootLayout.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        ));

        // 蓝牙图标
        ivIcon = new ImageView(context);
        ivIcon.setImageResource(R.drawable.bluetooth_icon);
        ivIcon.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

        // 默认内边距
        int padding = dpToPx(8);
        ivIcon.setPadding(padding, padding, padding, padding);

        // 添加到容器
        rootLayout.addView(ivIcon);
        this.rootView = rootLayout;

        // 初始化背景样式
        initBackgroundStyles(context);

        // 点击跳转蓝牙设置
        rootLayout.setOnClickListener(v -> {
            try {
                Intent intent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            } catch (ActivityNotFoundException e) {
                Log.e("BluetoothComponent", "Bluetooth settings not found");
            }
        });
    }

    private void initBackgroundStyles(Context context) {
        // 默认背景（透明）
        defaultBg = new GradientDrawable();
        defaultBg.setShape(GradientDrawable.OVAL);
        defaultBg.setColor(Color.TRANSPARENT);
        defaultBg.setStroke(dpToPx(1), 0x333333);

        // 选中背景（蓝色圆形边框）
        selectedBg = new GradientDrawable();
        selectedBg.setShape(GradientDrawable.OVAL);
        selectedBg.setColor(0x33AEDEFE); // 20% 透明度蓝色
        selectedBg.setStroke(dpToPx(2), 0xFF2196F3);

        rootView.setBackground(defaultBg);
    }

    @Override
    protected void updateSelectionUI(boolean selected) {
        // 切换背景动画
        ivIcon.animate()
                .scaleX(selected ? 1.1f : 1.0f)
                .scaleY(selected ? 1.1f : 1.0f)
                .setDuration(150)
                .start();

        rootView.setBackground(selected ? selectedBg : defaultBg);
    }

    @Override
    public void setHeight(int heightPx) {
        // 同时设置宽度保证正方形
        ViewGroup.LayoutParams params = rootView.getLayoutParams();
        if (params == null) {
            params = new ViewGroup.LayoutParams(heightPx, heightPx);
        } else {
            params.width = heightPx;
            params.height = heightPx;
        }
        rootView.setLayoutParams(params);

        // 调整图标尺寸为 70% 高度
        ViewGroup.LayoutParams iconParams = ivIcon.getLayoutParams();
        if (iconParams == null) {
            iconParams = new FrameLayout.LayoutParams(
                    (int)(heightPx * 0.7),
                    (int)(heightPx * 0.7),
                    Gravity.CENTER
            );
        } else {
            iconParams.width = (int)(heightPx * 0.7);
            iconParams.height = (int)(heightPx * 0.7);
        }
        ivIcon.setLayoutParams(iconParams);
    }

    @Override
    public void setOnComponentClickListener(View.OnClickListener listener) {

    }

    private int dpToPx(int dp) {
        return (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                dp,
                rootView.getResources().getDisplayMetrics()
        );
    }
}
